package cn.wolfcode.wolf2w.service.impl;

import cn.wolfcode.wolf2w.domain.Destination;
import cn.wolfcode.wolf2w.domain.StrategyCatalog;
import cn.wolfcode.wolf2w.query.StrategyCatalogQuery;
import cn.wolfcode.wolf2w.repository.DestinationRepository;
import cn.wolfcode.wolf2w.repository.StrategyCatalogRepository;

import cn.wolfcode.wolf2w.service.IStrategyCatalogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

@Service
public class StrategyCatalogServiceImpl implements IStrategyCatalogService {

    @Autowired
    private StrategyCatalogRepository strategyCatalogRepository;

    @Autowired
    private DestinationRepository destinationRepository;
    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public void save(StrategyCatalog strategyCatalog) {
        strategyCatalog.setId(null);
        strategyCatalogRepository.save(strategyCatalog);
    }

    @Override
    public void delete(String id) {

        strategyCatalogRepository.deleteById(id);
    }

    @Override
    public void update(StrategyCatalog strategyCatalog) {

        // 当实现修改操作时,如果数据库列表中的字段和 domain里面的一致就
        // 不用先查后修改操作.
        /*String id = strategyCatalog.getId();
        StrategyCatalog newStrategyCatalog = mongoTemplate.findById(id, StrategyCatalog.class);*/
        strategyCatalogRepository.save(strategyCatalog);
    }

    @Override
    public StrategyCatalog get(String id) {
       return strategyCatalogRepository.findById(id).orElse(null);
    }

    @Override
    public Page<StrategyCatalog> query(StrategyCatalogQuery qo) {
        // 创建查询对象
        Query query = new Query();
        // 查询攻略分类的总数据数
        long totalCount = mongoTemplate.count(query, StrategyCatalog.class);
        // 如果为
        if (totalCount == 0){
            return Page.empty();
        }
        // 创建分页添加
        Pageable pageable = PageRequest.of(qo.getCurrentPage() - 1, qo.getPageSize(),
                Sort.Direction.ASC, "_id");

        query.with(pageable);
        // 查询带条件的数据库数据
        List<StrategyCatalog> list = mongoTemplate.find(query, StrategyCatalog.class);
        // 用PageImpl实现Page接口, 可得到页面想要的分页数据
        return new PageImpl<>(list,pageable,totalCount);
    }

    @Override
    public void saveOrUpdate(StrategyCatalog strategyCatalog) {
        // 在添加与编辑的模态框中存在关联目的地下拉框(回显)
        // 通过攻略分类对象获取到 目的地id
        String destId = strategyCatalog.getDestId();
        // 通过目的地id查询到 目的地管理对象(此处直接注入目的地实现方法即可)
        Destination destination = destinationRepository.findById(destId).orElse(null);
        // 将获取到的目的地name 设入攻略分类对象中,从而达到回显
        strategyCatalog.setDestName(destination.getName());

        // 通过id是否为null  判断是编辑和添加
        if (StringUtils.hasLength(strategyCatalog.getId())){
           this.update(strategyCatalog);
       }else{
           this.save(strategyCatalog);
       }
    }




}
